/*
 * Copyright (C) 2023 Coder.AN
 * Email: an.hongjun@foxmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "edge_tensor.h"
#include "edge_tensor_kernel.h"
#include <stdio.h>

namespace rect{
__device__ void cal_dim(int idx, int* n, int* h, int* w, int*c, Dim dim, DimOrder order)
{
    if (order == NHWC)
    {
        *n = idx / (dim.H * dim.W * dim.C);
        idx -= *n * dim.H * dim.W * dim.C;
        *h = idx / (dim.W * dim.C);
        idx -= *h * dim.W * dim.C;
        *w = idx / dim.C;
        idx -= *w * dim.C;
        *c = idx;
    }
    else if (order == NCWH)
    {
        *n = idx / (dim.C * dim.W * dim.H);
        idx -= *n * dim.C * dim.W * dim.H;
        *c = idx / (dim.W * dim.H);
        idx -= *c * dim.W * dim.H;
        *w = idx / dim.H;
        idx -= *w * dim.H;
        *h = idx;
    }
    else if (order == NCHW)
    {
        *n = idx / (dim.C * dim.H * dim.W);
        idx -= *n * dim.C * dim.H * dim.W;
        *c = idx / (dim.H * dim.W);
        idx -= *c * dim.H * dim.W;
        *h = idx / dim.W;
        idx -= *h * dim.W;
        *w = idx;
    }
}

__device__ int cal_new_idx(int n, int h, int w, int c, Dim dim, DimOrder order)
{
    if (order == NHWC)
    {
        return n * dim.H * dim.W * dim.C + h * dim.W * dim.C + w * dim.C + c;
    }
    else if (order == NCWH)
    {
        return n * dim.C * dim.W * dim.H + c * dim.W * dim.H + w * dim.H + h;
    }
    else if (order == NCHW)
    {
        return n * dim.C * dim.H * dim.W + c * dim.H * dim.W + h * dim.W + w;
    }
    return 0;
}
}

__global__ void Rect_kernel(const void* src, void* dst, int delta_w, int delta_h, int new_w, int new_h,
                            Dim old_dim, Dim new_dim, DimOrder dim_order, size_t unit_size, size_t unit_count) 
{
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= unit_count) return;

    int n, h, w, c;
    rect::cal_dim(idx, &n, &h, &w, &c, new_dim, dim_order);

    int old_w = w + delta_w;
    int old_h = h + delta_h;

    if (old_w >= old_dim.W) return;
    if (old_h >= old_dim.H) return;

    int old_idx = rect::cal_new_idx(n, old_h, old_w, c, old_dim, dim_order);

    if (unit_size == 1)
    {
        ((uint8_t*)dst)[idx] = ((uint8_t*)src)[old_idx];
    }
    else if (unit_size == 2)
    {
        ((uint16_t*)dst)[idx] = ((uint16_t*)src)[old_idx];
    }
    else if (unit_size == 4)
    {
        ((uint32_t*)dst)[idx] = ((uint32_t*)src)[old_idx];
    }
    else if (unit_size == 8)
    {
        ((uint64_t*)dst)[idx] = ((uint64_t*)src)[old_idx];
    }
}

void Rect(const void* src, void* dst, int delta_w, int delta_h, int new_w, int new_h,
            Dim old_dim, DimOrder dim_order, size_t unit_size, size_t unit_count)
{
    dim3 block(1024);
    dim3 grid((unit_count - 1) / block.x + 1);
    Dim new_dim(old_dim.N, old_dim.C, new_w, new_h);
    Rect_kernel<<<grid, block>>>(src, dst, delta_w, delta_h, new_w, new_h,
                                    old_dim, new_dim, dim_order, unit_size, unit_count);
    CHECK(cudaDeviceSynchronize());
    return;
}  
